package com.example.myone.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Activity.ArticleActivity;
import com.example.myone.Entity.Article;
import com.example.myone.R;
import com.example.myone.Util.ImageUtils;

import java.util.List;

public class MyArticleAdapter extends RecyclerView.Adapter<MyArticleAdapter.ListArticleViewHolder> {
    public List<Article> articleList;
    public Context context;

    public MyArticleAdapter(Context context, List<Article> articleList) {
        this.context = context;
        this.articleList = articleList;
    }

    @NonNull
    @Override
    public ListArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.my_pager_article_item, parent, false);
        return new ListArticleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListArticleViewHolder holder, int position) {
        Article article = articleList.get(position);
        ImageUtils.setImageByBase64Str(context, holder.cover, article.cover);
        holder.title.setText(article.title);
        holder.linearLayout.setOnClickListener(v -> {
            Toast.makeText(context, article.title, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, ArticleActivity.class);
            intent.putExtra("article_id", article.id);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return articleList.size();
    }

    public static class ListArticleViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView cover;
        LinearLayout linearLayout;

        public ListArticleViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            cover = itemView.findViewById(R.id.cover);
            linearLayout = itemView.findViewById(R.id.article_title_box);
        }
    }
}
